package org.example.mpdemo1.controller;

import org.example.mpdemo1.common.Result;
import org.example.mpdemo1.entity.Ams;
import org.example.mpdemo1.entity.Instrument;
import org.example.mpdemo1.mapper.InstrumentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping(value="/instrument")//请求网址
public class InstrumentController {
    @Autowired
    private InstrumentMapper instrumentMapper;

    @GetMapping
    public Result query() {
        List<Instrument> list=instrumentMapper.getAll();
        return Result.success(list);
    }

    @PostMapping("/search")
    public Result findBySearch(@RequestBody Ams ams){
        if (ams.getState()==null){
            List<Instrument> info = instrumentMapper.findByName(ams.getName());
            return Result.success(info);
        }
        else {
            List<Instrument> info = instrumentMapper.findBySearch(ams);
            return Result.success(info);
        }
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable int id){
        System.out.println(id);
        instrumentMapper.delete1(id);
        return Result.success();
    }

    @PostMapping
    public Result save(@RequestBody Instrument instrument){
        if(instrument.getId()==null){
            System.out.println(instrument.getState());
            instrumentMapper.insertIn(instrument);
        }else{
            instrumentMapper.updateBySdKey(instrument);
        }
        return Result.success();
    }

    @GetMapping("/{id}")
    public Instrument selectById(@PathVariable int id){
        return instrumentMapper.selectById(id);
    }
}
